package com.wkq.move.module.tv.frame.view

import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.jzvd.JzvdStd
import com.bumptech.glide.Glide
import com.google.android.material.appbar.AppBarLayout
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.base.utils.AlertUtil
import com.wkq.lib_base.adapter.DataBindingAdapter
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.wkq.lib_base.utlis.StatusBarUtil
import com.wkq.move.R
import com.wkq.move.module.main.ui.activity.WebViewActivity
import com.wkq.move.module.tv.ui.activity.FullActivity
import com.wkq.move.module.tv.ui.activity.UsTvShowActivity
import com.wkq.move.module.tv.ui.adapter.TvAboutAdapter
import com.wkq.move.module.tv.ui.adapter.TvActorAdapter
import com.wkq.move.module.tv.ui.adapter.TvSiteAdapter
import com.wkq.move.module.video.FullScreenActivity
import com.wkq.net.model.UsTvDetailInfo
import com.wkq.net.model.UsTvDetailInfo.PlayOnLineBean
import com.wkq.net.model.UsTvPlayInfo
import kotlinx.android.synthetic.main.activity_us_tv_show.*


/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/5/18
 *
 * 用途:
 */


class UsTvShowView : MvpView {

    var poster:String? =null
    var mActivity: UsTvShowActivity

    var tvSiteAdapter: TvSiteAdapter? = null
    var tvActorAdapter: TvActorAdapter? = null
    var tvAboutAdapter: TvAboutAdapter? = null


    constructor(mActivity: UsTvShowActivity) {
        this.mActivity = mActivity
    }

    fun initView() {
        initToolBar()
        initSite()
    }

    private fun initSite() {
        mActivity.binding.rvSite.layoutManager = LinearLayoutManager(mActivity)
        tvSiteAdapter = TvSiteAdapter(mActivity, R.layout.item_tv_site)
        mActivity.binding.rvSite.adapter = tvSiteAdapter


        mActivity.binding.rvAbout.layoutManager = GridLayoutManager(mActivity, 3)
        tvAboutAdapter = TvAboutAdapter(mActivity, R.layout.item_tv_about)
        mActivity.binding.rvAbout.adapter = tvAboutAdapter

        tvAboutAdapter?.setOnViewClickListener(object :
            KtDataBindingAdapter.OnAdapterViewClickListener<UsTvDetailInfo.TvAboutInfo> {
            override fun onViewClick(v: View?, program: UsTvDetailInfo.TvAboutInfo?) {
                if (mActivity.getPresenter() != null) mActivity.getPresenter().initData(program?.tvAboutHref)
            }
        })

        tvSiteAdapter?.setOnItemClickListener(object : TvSiteAdapter.OnItemClickListener {
            override fun onItemClick(program: UsTvDetailInfo.PlayOnLineInfo?) {
                if (program!!.isPlayState.equals("1")) {
                    openPlay(program?.playOnlinePath)
                } else if (program!!.isPlayState.equals("2")) {
                    if (mActivity.getPresenter() != null) mActivity.getPresenter().initData(program?.playOnlinePath)
                } else {
                    openXunLei(program?.playOnlinePath)
                }

            }
        })

        mActivity.binding.rvActor.layoutManager =
            LinearLayoutManager(mActivity, RecyclerView.HORIZONTAL, false)
        tvActorAdapter = TvActorAdapter(mActivity, R.layout.item_tv_actor)
        mActivity.binding.rvActor.adapter = tvActorAdapter
    }


    fun showView(info: UsTvDetailInfo?) {
        mActivity.binding.appbarLayout.setExpanded(true);
        mActivity.binding.data = info
        var playOnLineBeanList: List<PlayOnLineBean>? =
            info?.playOnLineBeanList?.dropLastWhile { it.playOnLineInfoList == null || it.playOnLineInfoList.size == 0 }

        if (playOnLineBeanList != null) {

            tvSiteAdapter?.addItems(playOnLineBeanList?.toMutableList())
            mActivity.ll_site.visibility = View.VISIBLE
        } else {
            mActivity.ll_site.visibility = View.GONE
        }
        if (info?.actorInfos != null) {
            tvActorAdapter?.addItems(info?.actorInfos)
            mActivity.ll_actor.visibility = View.VISIBLE
        } else {
            mActivity.ll_actor.visibility = View.GONE
        }
        if (info?.tvAboutInfoList != null) {
            tvAboutAdapter?.addItems(info?.tvAboutInfoList)
            mActivity.ll_about.visibility = View.VISIBLE
        } else {
            mActivity.ll_about.visibility = View.GONE
        }


        if (TextUtils.isEmpty(info?.des) || info?.des?.length!! < 10) {
            mActivity.ll_desc.visibility = View.GONE
        } else {
            mActivity.ll_desc.visibility = View.VISIBLE
        }
        if (TextUtils.isEmpty(info?.doubanSite)) {
            mActivity.ll_douban.visibility = View.GONE
        } else {
            mActivity.ll_douban.visibility = View.VISIBLE
        }


        poster=info?.tvPoster
        Glide.with(mActivity).load(info?.tvPoster).into(mActivity.binding.ivIcon)
        Glide.with(mActivity).load(info?.tvPoster).into(mActivity.binding.ivDrop)

        mActivity.binding.tvDouban.setOnClickListener {
            WebViewActivity.Companion.newInstance(
                mActivity,
                info?.doubanPath
            )
        }
    }


    private fun openPlay(playOnlinePath: String?) {
        mActivity.presenter.getPlayPath(playOnlinePath)
    }

    fun initToolBar() {
        StatusBarUtil.setTransparentForWindow(mActivity)
        StatusBarUtil.addTranslucentView(mActivity, 0)
        StatusBarUtil.setDarkMode(mActivity)
        mActivity.binding.appbarLayout.addOnOffsetChangedListener(object :
            AppBarLayout.OnOffsetChangedListener {
            override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
                if (Math.abs(verticalOffset) >= appBarLayout!!.totalScrollRange) {
                    if (!mActivity.isExpend) {
                        mActivity.binding.tvTitle.visibility = View.VISIBLE
                        mActivity.isExpend = !mActivity.isExpend
                    }
                } else {
                    if (mActivity.isExpend) {
                        mActivity.binding.tvTitle.visibility = View.GONE
                        mActivity.isExpend = !mActivity.isExpend
                    }
                }
            }
        })
        mActivity.binding.rlBack.setOnClickListener { mActivity.finish() }


    }

    fun openXunLei(path: String?) {
        try {
            var intent = Intent(Intent.ACTION_VIEW, Uri.parse(path));
            intent.addCategory("android.intent.category.DEFAULT");
            mActivity.startActivity(intent);
        } catch (e: Exception) {
            showMessage("暂未安装")
        }

    }

    fun showFail() {
        showMessage("数据异常")
        mActivity.finish()
    }

    fun showMessage(message: String?) {
        if (mActivity == null || TextUtils.isEmpty(message)) return
        AlertUtil.showDeftToast(mActivity, message)
    }

    fun playTv(playInfo: UsTvPlayInfo?) {
        if (playInfo != null && playInfo!!.playPath!!.split("url=").size >= 2) {
            var path: String = playInfo!!.playPath!!.split("url=")[1]
            FullScreenActivity.newInstaceFullActivity(mActivity,playInfo.playTitle,poster,path)

        } else {

            if (playInfo != null) {
                FullScreenActivity.newInstaceFullActivity(mActivity,playInfo.playTitle,poster!!,playInfo.playPath)
            }
        }


    }

}